package com.siruier.boss.ui.activity.task

import android.os.Bundle
import android.view.View
import androidx.core.view.isGone
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.DividerItemDecoration
import com.siruier.boss.R
import com.siruier.boss.api.core.launchUI
import com.siruier.boss.api.core.toData
import com.siruier.boss.api.taskApi
import com.siruier.boss.bean.TaskAuditDetail
import com.siruier.boss.bean.UserHisTask
import com.siruier.boss.common.formatPriceRmb
import com.siruier.boss.databinding.ActivityBaseRefreshBinding
import com.siruier.boss.databinding.ActivityTaskRecordBinding
import com.siruier.boss.databinding.ItemTaskBinding
import com.siruier.boss.ui.base.BaseActivity
import com.siruier.boss.ui.base.BaseFragment
import com.siruier.boss.ui.base.BaseRecyclerAdapter
import com.siruier.boss.ui.ext.tabData
import com.siruier.boss.ui.helper.RefreshHelper
import com.siruier.boss.ui.listener.OnTabSelectedListener
import com.siruier.boss.ui.utils.FragmentChangeManager
import com.siruier.boss.ui.utils.GlideUtils

class TaskRecordActivity : BaseActivity<ActivityTaskRecordBinding>() {
    private var mFragmentChangeManager: FragmentChangeManager? = null
    override fun initView() {
        vb.tabLayout.tabData {
            tab { "审核记录" }
            tab { "发放记录" }
        }
        vb.tabLayout.addOnTabSelectedListener(OnTabSelectedListener {
            mFragmentChangeManager?.setFragments(it.position)
        })
        val fragments = ArrayList<Fragment>()
        fragments.add(TaskAuditRecordFragment())
        fragments.add(TaskGrantRecordFragment())
        mFragmentChangeManager = FragmentChangeManager(supportFragmentManager, R.id.frame_layout, fragments)
    }
}

class TaskAuditRecordFragment : BaseFragment<ActivityBaseRefreshBinding>() {
    private val mTaskAuditDetailAdapter = TaskAuditDetailAdapter()
    private lateinit var mRefreshHelper: RefreshHelper<TaskAuditDetail>
    override fun initView(rootView: View?, savedInstanceState: Bundle?) {
        vb.titleView.isGone = true
        mRefreshHelper = RefreshHelper(vb.refreshLayout, vb.recyclerView, mTaskAuditDetailAdapter, listener = ::onReload)
        vb.recyclerView.addItemDecoration(DividerItemDecoration(context, DividerItemDecoration.VERTICAL))
        mRefreshHelper.onRefresh()
    }

    private fun onReload() {
        launchUI({
            taskApi.auditTaskList(mRefreshHelper.page).toData()?.records
        }, mRefreshHelper.handApiObserver())
    }

    class TaskAuditDetailAdapter : BaseRecyclerAdapter<ItemTaskBinding, TaskAuditDetail>() {

        override fun onBindViewHolder(vb: ItemTaskBinding, bean: TaskAuditDetail, position: Int) {
            GlideUtils.intoView(vb.ivIcon.context, bean.appIcon, vb.ivIcon)
            vb.tvName.text = bean.appName
            vb.tvMoney.text = bean.income.formatPriceRmb()
            vb.tvTotalNum.text = bean.createDate + "    " + bean.auditErrorMsg
            vb.tvRunning.text = when (bean.taskState) {
                1 -> "审核中"
                2 -> "审核失败"
                3 -> "审核成功"
                else -> ""
            }
            setViewClick(position, vb.root)
        }
    }
}

class TaskGrantRecordFragment : BaseFragment<ActivityBaseRefreshBinding>() {
    private val mTaskAuditDetailAdapter = TaskAuditDetailAdapter()
    private lateinit var mRefreshHelper: RefreshHelper<UserHisTask>
    override fun initView(rootView: View?, savedInstanceState: Bundle?) {
        vb.titleView.isGone = true
        mRefreshHelper = RefreshHelper(vb.refreshLayout, vb.recyclerView, mTaskAuditDetailAdapter, listener = ::onReload)
        vb.recyclerView.addItemDecoration(DividerItemDecoration(context, DividerItemDecoration.VERTICAL))
        mRefreshHelper.onRefresh()
    }

    private fun onReload() {
        launchUI({
            taskApi.hisTaskList(mRefreshHelper.page).toData()?.records
        }, mRefreshHelper.handApiObserver())
    }

    class TaskAuditDetailAdapter : BaseRecyclerAdapter<ItemTaskBinding, UserHisTask>() {

        override fun onBindViewHolder(vb: ItemTaskBinding, bean: UserHisTask, position: Int) {
            GlideUtils.intoView(vb.ivIcon.context, bean.appIcon, vb.ivIcon)
            vb.tvName.text = bean.appName
            vb.tvMoney.text = bean.changeMoney.formatPriceRmb()
            vb.tvTotalNum.text = bean.date
            setViewClick(position, vb.root)
        }
    }
}